package com.itdct.onflow.system.task.listener

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.itdct.onflow.core.config.OnflowConfig
import com.itdct.onflow.core.extend.logInfo
import com.itdct.onflow.system.task.entity.SysTaskEntity
import com.itdct.onflow.system.task.manager.SysTaskManager
import com.itdct.onflow.system.task.mapper.SysTaskMapper
import org.springframework.context.ApplicationListener
import org.springframework.context.event.ContextRefreshedEvent
import org.springframework.stereotype.Component

/**
 * @author Zhouwx
 * @date 2025/9/22 10:13:01
 * @version 1.0
 * @description
 */
@Component
class SysTaskListener(
    val sysTaskMapper: SysTaskMapper,
    val sysTaskManager: SysTaskManager,
    val onflowConfig: OnflowConfig
) : ApplicationListener<ContextRefreshedEvent> {

    override fun onApplicationEvent(event: ContextRefreshedEvent) {
        logInfo("定时任务初始化开始")
        val ktQueryWrapper = KtQueryWrapper(SysTaskEntity::class.java)
        ktQueryWrapper.eq(SysTaskEntity::platformType, onflowConfig.platformType)
        val sysTaskEntities = sysTaskMapper.selectList(ktQueryWrapper)
        sysTaskEntities.forEach {
            sysTaskManager.runTask(it)
        }
        logInfo("定时任务初始化完成")
    }
}